package codeGeneration.service;

import java.io.FileWriter;
import java.io.IOException;

import util.Common;
import util.FileUtil;
import util.HeaderOfJavaFile;
import util.InformationForGenerate;
import codeGeneration.Generator;

public class ServiceGenerator extends Generator {
	private String classDAOName;
	private String objDAOName;
	private String packageName;
	private String serviceName;
	private InformationForGenerate informationForGenerate;
	private StringBuffer serviceBuffWriter;
	private String serviceFileLocation;
	private HeaderOfJavaFile headerOfJavaFile;

	public ServiceGenerator(InformationForGenerate informationForGenerate,
			String beanName, String packageName, String srcPath) {
		super();

		this.packageName = packageName;
		this.informationForGenerate = informationForGenerate;

		classDAOName = informationForGenerate.getNameOfDAOClass();
		objDAOName = Common.convertFirstLetterToLowerCase(classDAOName);

		serviceName = beanName + "Service";
		informationForGenerate.setNameOfServiceClass(serviceName);

		serviceFileLocation = srcPath + '/' + packageName.replace('.', '/');
		FileUtil.mkDir(serviceFileLocation);

		headerOfJavaFile = new HeaderOfJavaFile();
		serviceBuffWriter = new StringBuffer();
		indentOneTime(HeaderOfJavaFile.packageNamePlaceHolder, null, ";\n",
				serviceBuffWriter, true);
		indentOneTime(HeaderOfJavaFile.importsListPlaceHolder, null, "",
				serviceBuffWriter, true);

	}

	public void generateService() throws IOException {
		generateClass();
		replacePlaceHolder(serviceBuffWriter, headerOfJavaFile);
		FileWriter fileWriter = null;

		try {
			fileWriter = getFileWriter(serviceFileLocation + '/' + serviceName
					+ ".java");
			fileWriter.append(serviceBuffWriter);
		} catch (IOException e) {
			e.printStackTrace();
			throw new IOException("Can't interact with file in service layer");
		}

		if (fileWriter != null)
			closeFileWriter(fileWriter);
	}

	private void generateClass() {
		headerOfJavaFile.setPackageName(packageName);
		informationForGenerate.setPackageOfService(packageName);
		headerOfJavaFile
				.addImportsList("org.springframework.stereotype.Service");
		appendToFile(serviceBuffWriter, "@Service\n");
		indentOneTime("public class " + serviceName, null, " {",
				serviceBuffWriter, false);

		generateProperties();
		generateAutoWireDAO();
		generateExtra();

		decreaseIndentOneTime("}", serviceBuffWriter);
	}

	private void generateProperties() {
		headerOfJavaFile.addImportsList(informationForGenerate
				.getPackageOfDAO()
				+ '.'
				+ informationForGenerate.getNameOfDAOClass());
		
		appendToFile(serviceBuffWriter, "private " + classDAOName + ' '
				+ objDAOName + ";\n\n");

	}

	private void generateAutoWireDAO() {
		headerOfJavaFile.addImportsList("org.springframework.beans."
				+ "factory.annotation.Autowired");
		appendToFile(serviceBuffWriter, "@Autowired\n");
		indentOneTime("public void set" + classDAOName, null, "("
				+ classDAOName + ' ' + objDAOName + ") {", serviceBuffWriter,
				false);

		appendToFile(serviceBuffWriter, "this." + objDAOName + " = "
				+ objDAOName + ";\n");
		decreaseIndentOneTime("}\n", serviceBuffWriter);
	}

	private void generateExtra() {
		appendToFile(serviceBuffWriter,
				"// TODO : Write code by yourself here\n");
	}
}